<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 账号管理 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="danger" icon="el-icon-delete" class="handle-del mr10"
                    @click="delAllSelection">批量删除</el-button>
                <el-button type="primary" class="handle-del mr10" @click="addDialog">新增账号</el-button>
            </div>
            <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header"
                @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="username" label="账号"></el-table-column>
                <el-table-column prop="group_title" label="角色"></el-table-column>
                <el-table-column prop="create_time" label="加入时间"></el-table-column>
                <el-table-column label="状态">
                    <template slot-scope="scope">
                        {{ scope.row.status == 0 ? '禁用' : '启用' }}
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-row>
                            <el-button @click="edit(scope.row)" type="success" size="mini">编辑</el-button>
                            <el-button @click="disable(scope.row.id, scope.row.status)" type="warning" size="mini">
                                {{ scope.row.status == 0 ? '启用' : '禁用' }}
                            </el-button>
                            <el-button @click="deleteRow(scope.row.id, scope.$index)" type="danger"
                                size="mini">删除</el-button>
                        </el-row>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background layout="total, prev, pager, next" :current-page="query.page"
                    :page-size="query.rows" :total="pageTotal" @current-change="handlePageChange"></el-pagination>
            </div>
        </div>
        <!-- 新增账号弹框 -->
        <el-dialog title="新增/编辑 账号" @close="exit" :visible.sync="showDialog" width="30%">
            <el-form ref="ruleForm" :model="ruleForm" status-icon :rules="rules" label-width="100px">
                <el-form-item label="账号" prop="username">
                    <el-input v-model="ruleForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPass">
                    <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="角色" prop="group_id">
                    <el-select v-model="ruleForm.group_id" clearable placeholder="请选择角色">
                        <el-option v-for="item in options" :key="item.id" :label="item.title" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-radio-group v-model="ruleForm.status">
                        <el-radio :label="1">启用</el-radio>
                        <el-radio :label="0">禁用</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="save">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { accountListData, accountLimitsListData, addAccountData, delAccountData } from '../../api/index';
export default {
    // name: 'suggest',
    data() {
        let validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                if (this.ruleForm.checkPass !== '') {
                    this.$refs.ruleForm.validateField('checkPass');
                }
                callback();
            }
        };
        let validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.ruleForm.password) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            query: {
                page: 1,
                rows: 4
            },
            ruleForm: {
                username: '',
                password: '',
                checkPass: '',
                group_id: '',
                status: ''
            },
            rules: {
                username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
                password: [{ required: true, validator: validatePass, trigger: 'blur' }],
                checkPass: [{ required: true, validator: validatePass2, trigger: 'blur' }],
                group_id: [{ required: true, message: '请选择角色', trigger: 'blur' }],
                status: [{ required: true, message: '请选择状态', trigger: 'blur' }]
            },
            options: [],
            tableData: [],
            multipleSelection: [],
            delList: [],
            showDialog: false,
            pageTotal: 0
        };
    },
    created() {
        this.getData();
    },
    methods: {
        getData() {
            accountListData(this.query).then((res) => {
                this.tableData = res.data.list;
                this.pageTotal = res.data.count;
            });
        },
        //新增
        addDialog() {
            this.showDialog = true;
            accountLimitsListData().then((res) => {
                this.options = res.data;
            });
        },
        //关闭
        exit() {
            this.ruleForm = {};
        },
        save() {
            addAccountData(this.ruleForm).then((res) => {
                //console.log(this.ruleForm);
                if (res.code == 1) {
                    this.showDialog = false;
                    this.$message.success('操作成功');
                    this.getData();
                    // setTimeout(() => {
                    //     location.reload();
                    // }, 200);
                }
            });
        },
        edit(row) {
            this.addDialog();
            this.ruleForm = row;
        },
        disable(id, status) {
            this.$confirm(`确定${status == 0 ? '启用' : '禁用'}吗！`, '提示', {
                type: 'warning'
            }).then(() => {
                addAccountData({ id: id, status: status == 0 ? 1 : 0 }).then((res) => {
                    if (res.code == 1) {
                        this.$message.success('操作成功');
                        this.getData();
                        // setTimeout(() => {
                        //     location.reload();
                        // }, 200);
                    }
                });
            });
        },
        //删除
        deleteRow(id, index) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                delAccountData({ id: id }).then((res) => {
                    if (res.code == 1) {
                        this.$message.success('删除成功');
                        this.tableData.splice(index, 1);
                        this.getData();
                        // setTimeout(() => {
                        //     location.reload();
                        // }, 200);
                    }
                });
            });
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        // 批量删除
        delAllSelection() {
            this.$confirm('确定要批量删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                const length = this.multipleSelection.length;
                let str = '';
                let strID = '';
                this.delList = this.delList.concat(this.multipleSelection);
                for (let i = 0; i < length; i++) {
                    str += this.multipleSelection[i].username + ' ';
                    strID += this.multipleSelection[i].id + ',';
                }
                delAccountData({ id: strID.substring(0, strID.lastIndexOf(',')) }).then((res) => {
                    if (res.code == 1) {
                        this.$message.success(`删除了${str}`);
                        this.multipleSelection = [];
                        this.getData();
                        // setTimeout(() => {
                        //     location.reload();
                        // }, 200);
                    }
                });
            });
        },
        // 分页
        handlePageChange(val) {
            this.$set(this.query, 'page', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
    float: left;
}

.handle-input {
    width: 300px;
    display: inline-block;
}

.table {
    width: 100%;
    font-size: 14px;
}

.mr10 {
    margin-right: 10px;
}
</style>


